package com.shsoft.component;

import com.shsoft.config.PCStatusConstant;
import com.shsoft.entity.HardWareInfo;
import com.shsoft.util.FileUtil;
import javafx.geometry.Bounds;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

public class DataComponent extends BaseComponent{

    private String name;

    private boolean unit;

    private String fontName;

    private int fontSize;

    private String alignment;

    private double colorR,colorG,colorB,colorA;

    private int borderX,borderY;

    private int borderWidth,borderHeight;

    @Override
    public void drawComponent(GraphicsContext gc) {
        gc.setFont(FileUtil.getFont(fontName, fontSize));
        if(alignment.equals(PCStatusConstant.ALIGNMENT_LEFT)){
            gc.setTextAlign(TextAlignment.LEFT);
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_CENTER)){
            gc.setTextAlign(TextAlignment.CENTER);
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_RIGHT)){
            gc.setTextAlign(TextAlignment.RIGHT);
        }
        gc.setFill(getColor());
        gc.fillText(getMaxContent(), getX(), getY());
    }

    @Override
    public void drawComponentToSmallCanvas(GraphicsContext gc) {
        gc.setFont(FileUtil.getFont(fontName, fontSize));
        gc.setFill(getColor());
        int tmpX,tmpY;
        tmpX = 0;
        tmpY = fontSize;
        if(fontName.equals("像素体") || fontName.equals("万事全兴体") || fontName.equals("GemunuLibre")){
            tmpY = fontSize - fontSize / 4;
        }else if(fontName.equals("抖音美好体") || fontName.equals("风雅黑体")){
            tmpY = fontSize - fontSize / 6;
        }else if(fontName.equals("琉璃体")){
            tmpY = fontSize - fontSize / 5;
        }else if(fontName.equals("鸿蒙体")){
            tmpY = fontSize - fontSize / 8;
        }else if(fontName.equals("思源宋体") || fontName.equals("数码数字体")){
            tmpY = fontSize - fontSize / 10;
        }
        if(alignment.equals(PCStatusConstant.ALIGNMENT_LEFT)){
            gc.setTextAlign(TextAlignment.LEFT);
            gc.fillText(getContent(), tmpX, tmpY);
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_CENTER)){
            gc.setTextAlign(TextAlignment.CENTER);
            gc.fillText(getContent(), tmpX + borderWidth / 2, tmpY);
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_RIGHT)){
            gc.setTextAlign(TextAlignment.RIGHT);
            gc.fillText(getContent(), tmpX + borderWidth, tmpY);
        }
    }

    @Override
    public void drawComponentBorder(Canvas canvas) {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        Text text = new Text();
        text.setFont(FileUtil.getFont(fontName, fontSize));
        text.setText(getMaxContent());
        Bounds layoutBounds = text.getLayoutBounds();
        borderHeight = fontSize;
        borderWidth = (int) layoutBounds.getWidth() + 2;
        borderY = getY() - fontSize;
        if(alignment.equals(PCStatusConstant.ALIGNMENT_LEFT)){
            borderX = getX();
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_CENTER)){
            borderX = getX() - (borderWidth - 2) / 2;
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_RIGHT)){
            borderX = getX() - borderWidth;
        }
        if(fontName.equals("像素体") || fontName.equals("万事全兴体") || fontName.equals("GemunuLibre")){
            borderHeight = borderHeight - fontSize / 8;
            borderY = borderY + fontSize / 4;
        }else if(fontName.equals("抖音美好体") || fontName.equals("风雅黑体")){
            borderY = borderY + fontSize / 6;
        }else if(fontName.equals("琉璃体")){
            borderY = borderY + fontSize / 5;
        }else if(fontName.equals("鸿蒙体")){
            borderY = borderY + fontSize / 8;
        }else if(fontName.equals("思源宋体") || fontName.equals("数码数字体")){
            borderHeight = borderHeight + fontSize / 8;
            borderY = borderY + fontSize / 10;
        }
        gc.setStroke(Color.GREY);
        gc.strokeRect(borderX, borderY, borderWidth, borderHeight);
    }

    @Override
    public boolean isMe(double x, double y) {
        if(x >= borderX && x <= borderX + borderWidth && y >= borderY && y <= borderY + borderHeight){
            return true;
        }
        return false;
    }


    public String getContent(){
        switch (name){
            case PCStatusConstant.COMPONENT_CPU_NAME:
                return HardWareInfo.cpuName;
            case PCStatusConstant.COMPONENT_CPU_TEMP:
                if(unit){
                    return HardWareInfo.cpuTempCur + " ℃";
                }else {
                    return HardWareInfo.cpuTempCur + "";
                }
            case PCStatusConstant.COMPONENT_CPU_USAGE:
                if(unit){
                    return HardWareInfo.cpuUsageCur + " %";
                }else {
                    return HardWareInfo.cpuUsageCur + "";
                }
            case PCStatusConstant.COMPONENT_CPU_CLOCK:
                if(unit){
                    return HardWareInfo.cpuClockCur + " MHz";
                }else {
                    return HardWareInfo.cpuClockCur + "";
                }
            case PCStatusConstant.COMPONENT_CPU_POWER:
                if(unit){
                    return HardWareInfo.df.format(HardWareInfo.cpuPowerCur) + " W";
                }else {
                    return HardWareInfo.df.format(HardWareInfo.cpuPowerCur) + "";
                }
            case PCStatusConstant.COMPONENT_GPU_NAME:
                return HardWareInfo.gpuName;
            case PCStatusConstant.COMPONENT_GPU_TEMP:
                if(unit){
                    return HardWareInfo.gpuTempCur + " ℃";
                }else {
                    return HardWareInfo.gpuTempCur + "";
                }
            case PCStatusConstant.COMPONENT_GPU_USAGE:
                if(unit){
                    return HardWareInfo.gpuUsageCur + " %";
                }else {
                    return HardWareInfo.gpuUsageCur + "";
                }
            case PCStatusConstant.COMPONENT_GPU_CLOCK:
                if(unit){
                    return HardWareInfo.gpuClockCur + " MHz";
                }else {
                    return HardWareInfo.gpuClockCur + "";
                }
            case PCStatusConstant.COMPONENT_GPU_POWER:
                if(unit){
                    return HardWareInfo.df.format(HardWareInfo.gpuPowerCur) + " W";
                }else {
                    return HardWareInfo.df.format(HardWareInfo.gpuPowerCur) + "";
                }
            case PCStatusConstant.COMPONENT_MEM_NAME:
                return HardWareInfo.memName;
            case PCStatusConstant.COMPONENT_MEM_USAGE:
                if(unit){
                    return HardWareInfo.memUsed + " %";
                }else {
                    return HardWareInfo.memUsed + "";
                }
            case PCStatusConstant.COMPONENT_MEM_CLOCK:
                if(unit){
                    return HardWareInfo.memClock + " MHz";
                }else {
                    return HardWareInfo.memClock + "";
                }
            case PCStatusConstant.COMPONENT_NET_UPLOAD_SPEED:
                if(HardWareInfo.netUploadSpeed < 100){
                    return HardWareInfo.df.format(HardWareInfo.netUploadSpeed) + " K/s";
                }else if(HardWareInfo.netUploadSpeed < 1000){
                    return (int)HardWareInfo.netUploadSpeed + " K/s";
                }else {
                    return HardWareInfo.df.format(HardWareInfo.netUploadSpeed / 1024) + " M/s";
                }
            case PCStatusConstant.COMPONENT_NET_DOWN_SPEED:
                if(HardWareInfo.netDownloadSpeed < 100){
                    return HardWareInfo.df.format(HardWareInfo.netDownloadSpeed) + " K/s";
                }else if(HardWareInfo.netDownloadSpeed < 1000){
                    return (int)HardWareInfo.netDownloadSpeed + " K/s";
                }else {
                    return HardWareInfo.df.format(HardWareInfo.netDownloadSpeed / 1024) + " M/s";
                }
            case PCStatusConstant.COMPONENT_DATE_YEAR:
                return HardWareInfo.getYear();
            case PCStatusConstant.COMPONENT_DATE_MONTH:
                return HardWareInfo.getMonth();
            case PCStatusConstant.COMPONENT_DATE_DAY:
                return HardWareInfo.getDay();
            case PCStatusConstant.COMPONENT_DATE_HOUR:
                return HardWareInfo.getHour();
            case PCStatusConstant.COMPONENT_DATE_MIN:
                return HardWareInfo.getMinute();
            case PCStatusConstant.COMPONENT_DATE_SEC:
                return HardWareInfo.getSecond();
            case PCStatusConstant.COMPONENT_DATE_WEEK:
                return HardWareInfo.getWeek();
            case PCStatusConstant.COMPONENT_WEATHER_TEMP:
                if(unit){
                    return HardWareInfo.cityTemp + " ℃";
                }else {
                    return HardWareInfo.cityTemp + "";
                }
            case PCStatusConstant.COMPONENT_WEATHER_HUM:
                if(unit){
                    return HardWareInfo.cityHum + " %";
                }else {
                    return HardWareInfo.cityHum + "";
                }
            case PCStatusConstant.COMPONENT_WEATHER_CITY:
                return HardWareInfo.cityName;
            case PCStatusConstant.COMPONENT_WEATHER_AIR:
                if(HardWareInfo.cityAqi <= 50){
                    return "优";
                }else if(HardWareInfo.cityAqi <= 100){
                    return "良";
                }else if(HardWareInfo.cityAqi <= 150){
                    return "中";
                }else{
                    return "差";
                }
            case PCStatusConstant.COMPONENT_WEATHER_STRING:
                return HardWareInfo.cityWeatherText;
            default:
                return "";
        }
    }

    public String getMaxContent(){
        switch (name){
            case PCStatusConstant.COMPONENT_CPU_NAME:
                return HardWareInfo.cpuName;
            case PCStatusConstant.COMPONENT_CPU_TEMP:
            case PCStatusConstant.COMPONENT_GPU_TEMP:
                if(unit){
                    return "100 ℃";
                }else {
                    return "100";
                }
            case PCStatusConstant.COMPONENT_CPU_USAGE:
            case PCStatusConstant.COMPONENT_GPU_USAGE:
            case PCStatusConstant.COMPONENT_MEM_USAGE:
            case PCStatusConstant.COMPONENT_WEATHER_HUM:
                if(unit){
                    return "100 %";
                }else {
                    return "100";
                }
            case PCStatusConstant.COMPONENT_CPU_CLOCK:
                if(unit){
                    return "5000 MHz";
                }else {
                    return "5000";
                }
            case PCStatusConstant.COMPONENT_CPU_POWER:
                if(unit){
                    return "180.0 W";
                }else {
                    return "180.0";
                }
            case PCStatusConstant.COMPONENT_GPU_NAME:
                return HardWareInfo.gpuName;
            case PCStatusConstant.COMPONENT_GPU_CLOCK:
                if(unit){
                    return "2800 MHz";
                }else {
                    return "2800";
                }
            case PCStatusConstant.COMPONENT_GPU_POWER:
                if(unit){
                    return "250.0 W";
                }else {
                    return "250.0";
                }
            case PCStatusConstant.COMPONENT_MEM_NAME:
                return HardWareInfo.memName;
            case PCStatusConstant.COMPONENT_MEM_CLOCK:
                if(unit){
                    return HardWareInfo.memClock + " MHz";
                }else {
                    return HardWareInfo.memClock + "";
                }
            case PCStatusConstant.COMPONENT_NET_UPLOAD_SPEED:
            case PCStatusConstant.COMPONENT_NET_DOWN_SPEED:
                return "100.0 K/s";
            case PCStatusConstant.COMPONENT_DATE_YEAR:
                return "0000";
            case PCStatusConstant.COMPONENT_DATE_MONTH:
            case PCStatusConstant.COMPONENT_DATE_DAY:
            case PCStatusConstant.COMPONENT_DATE_HOUR:
            case PCStatusConstant.COMPONENT_DATE_MIN:
            case PCStatusConstant.COMPONENT_DATE_SEC:
                return "00";
            case PCStatusConstant.COMPONENT_DATE_WEEK:
                return HardWareInfo.getWeek();
            case PCStatusConstant.COMPONENT_WEATHER_TEMP:
                if(unit){
                    return " 20 ℃";
                }else {
                    return " 20";
                }
            case PCStatusConstant.COMPONENT_WEATHER_CITY:
                return "XXXX";
            case PCStatusConstant.COMPONENT_WEATHER_AIR:
                if(HardWareInfo.cityAqi <= 50){
                    return "优";
                }else if(HardWareInfo.cityAqi <= 100){
                    return "良";
                }else if(HardWareInfo.cityAqi <= 150){
                    return "中";
                }else{
                    return "差";
                }
            case PCStatusConstant.COMPONENT_WEATHER_STRING:
                return "晴";
            default:
                return "";
        }
    }

    public int getBorderX() {
        return borderX;
    }

    public void setBorderX(int borderX) {
        this.borderX = borderX;
    }

    public int getBorderY() {
        return borderY;
    }

    public String getAlignment() {
        return alignment;
    }

    public void setAlignment(String alignment) {
        this.alignment = alignment;
    }

    public void setBorderY(int borderY) {
        this.borderY = borderY;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

    public int getBorderHeight() {
        return borderHeight;
    }

    public void setBorderHeight(int borderHeight) {
        this.borderHeight = borderHeight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setColor(Color color) {
        this.colorR = color.getRed();
        this.colorG = color.getGreen();
        this.colorB = color.getBlue();
        this.colorA = color.getOpacity();
    }

    public Color getColor() {
        return new Color(colorR,colorG,colorB,colorA);
    }

    public String getFontName() {
        return fontName;
    }

    public void setFontName(String fontName) {
        this.fontName = fontName;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public boolean isUnit() {
        return unit;
    }

    public void setUnit(boolean unit) {
        this.unit = unit;
    }

}
